function [ reversal] = isReversal( prevSktp,curSktp, length, prevSkewerAngle, curSkewerAngle )
%ISREVERSAL Determine whether the frame is a reversal
%  
%Input Arguments
%
% prevSktp - the skeleton point list at t-1
% curSktp - the skeleton point list at t
% length - the length of the current skeleton
% prevSkewerAngle - the skewer angle at t-1
% curSkewerAngle - the skewer angle at t
%
%  Returns - reversal = 1 if it is a reversal, 0 if not
     reversal = 0;
     % if the skewer angle between successive frames has changed less than
     % 10 degrees ...
     if curSkewerAngle - prevSkewerAngle < 10
         
         prevSktNumPts = size( prevSktp,1);
         curSktNumPts = size( curSktp,1);
         prevHead = prevSktp(1,:);
         prevTail = prevSktp(prevSktNumPts,:);
         curHead =  curSktp(1,:);
         curTail =  curSktp(curSktNumPts,:);
         
         %... and, if bwtween frame i-1 and i , the head skeleton point 
         %moves towards where the tail skeleton point was at time t-1 ...
         dist1 = distl2(prevHead, prevTail);
         dist2 = distl2(curHead, prevTail);
         if dist2 < dist1
             
             %.... and if between time i-1 and i  the tail skeleton point
             %moves away from where the head skeleton point was at 
             % time t-1....
             dist1 = distl2(prevTail, prevHead );
             dist2 = distl2(curTail, prevHead );
             if dist2 > dist1
                 
                 %and if the average distances moved by the head and tail
                 %during te time interval is larger than 2.5% of the body
                 %length ....
                 distHead =  distl2(prevHead, curHead);
                 distTail =  distl2(prevTail,curTail);
                 ave = (distHead + distTail)/2;
                 if ave < 0.025 * length;
                     reversal = 1;
                 end
             end
         end
     end
end

